import { createSelector } from '@reduxjs/toolkit';
import { stateSelector } from 'store/store';
import { useAppSelector } from 'store/storeHooks';
import { defaultSelectorOptions } from 'store/util/defaultMemoizeOptions';
import i18n from 'i18next';
// import { map } from 'lodash-es';

const selector = createSelector(
  [stateSelector],
  (
    { generation, system },
    
  ) => {
    const { model } = generation;

    const { isConnected } = system;

    const reasons: string[] = [];

    // Cannot generate if not connected
    if (!isConnected) {
      reasons.push(i18n.t('parameters.invoke.systemDisconnected'));
    }

    // if (dynamicPrompts.prompts.length === 0) {
    //   reasons.push(i18n.t('parameters.invoke.noPrompts'));
    // }

    if (!model) {
      reasons.push(i18n.t('parameters.invoke.noModelSelected'));
    }

    // if (controlNet.isEnabled) {
    //   map(controlNet.controlNets).forEach((controlNet, i) => {
    //     if (!controlNet.isEnabled) {
    //       return;
    //     }
    //     if (!controlNet.model) {
    //       reasons.push(
    //         i18n.t('parameters.invoke.noModelForControlNet', { index: i + 1 })
    //       );
    //     }

    //     if (
    //       !controlNet.controlImage ||
    //       (!controlNet.processedControlImage &&
    //         controlNet.processorType !== 'none')
    //     ) {
    //       reasons.push(
    //         i18n.t('parameters.invoke.noControlImageForControlNet', {
    //           index: i + 1,
    //         })
    //       );
    //     }
    //   });
    // }

    return { isReady: !reasons.length, reasons };
  },
  defaultSelectorOptions
);

export const useIsReadyToEnqueue = () => {
  const { isReady, reasons } = useAppSelector(selector);
  return { isReady, reasons };
};
